/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.buzz.sign.service;

import java.util.List;

import com.jeeplus.modules.adjunctFile.entity.AdjunctFile;
import com.jeeplus.modules.adjunctFile.mapper.AdjunctFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.buzz.sign.entity.Sign;
import com.jeeplus.modules.buzz.sign.mapper.SignMapper;
import com.jeeplus.modules.buzz.sign.entity.Signentry;
import com.jeeplus.modules.buzz.sign.mapper.SignentryMapper;

/**
 * 签到登记Service
 * @author 陈
 * @version 2025-07-06
 */
@Service
@Transactional(readOnly = true)
public class SignService extends CrudService<SignMapper, Sign> {

	@Autowired
	private SignentryMapper signentryMapper;
	@Autowired
	private AdjunctFileMapper adjunctFileMapper;


	public Sign get(String id) {
		Sign sign = super.get(id);
		sign.setSignentryList(signentryMapper.findList(new Signentry(sign)));
		sign.setAdjunctFileList(adjunctFileMapper.findList(new AdjunctFile("签到登记",sign.getId())));

		return sign;
	}

	public List<Sign> findList(Sign sign) {
		return super.findList(sign);
	}

	public Page<Sign> findPage(Page<Sign> page, Sign sign) {
		return super.findPage(page, sign);
	}

	@Transactional(readOnly = false)
	public void save(Sign sign) {
		super.save(sign);
		for (Signentry signentry : sign.getSignentryList()){
			if (signentry.getId() == null){
				continue;
			}
			if (Signentry.DEL_FLAG_NORMAL.equals(signentry.getDelFlag())){
				if (StringUtils.isBlank(signentry.getId())){
					signentry.setInteryid(sign);
					signentry.preInsert();
					signentryMapper.insert(signentry);
				}else{
					signentry.preUpdate();
					signentryMapper.update(signentry);
				}
			}else{
				signentryMapper.delete(signentry);
			}
		}
		for (AdjunctFile adjunctFile : sign.getAdjunctFileList()){
			if (StringUtils.isBlank(adjunctFile.getId())){
				adjunctFile.setObjectId("签到登记");
				adjunctFile.setDataId(sign.getId());
				adjunctFile.preInsert();
				adjunctFileMapper.insert(adjunctFile);
			}else{
				adjunctFile.preUpdate();
				adjunctFileMapper.update(adjunctFile);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(Sign sign) {
		super.delete(sign);
		signentryMapper.delete(new Signentry(sign));
		adjunctFileMapper.delete(new AdjunctFile("签到登记",sign.getId()));
	}

}
